package textviewer.main;
import java.util.ArrayList;

import textviewer.main.Config;
import textviewer.main.TCPGather;
import textviewer.main.Utility;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class GUIInternetForumTopicSelection extends Activity{
	private String forum_name;
	private String forum_url;
	private Config myconfig;
	private int total_page=0;
	private int curr_page=1;
	private String new_cookie;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        forum_name = extras.getString("forum_name");
        forum_url = extras.getString("forum_url");
        new_cookie = extras.getString("new_cookie");
        //curr_page
        curr_page = extras.getInt("curr_page", 1);
        //listenOnID = extras.getInt("listenOnID", 1);
        ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        
        ScrollView scllview = new ScrollView(this);
        scllview.setBackgroundColor(Color.WHITE);
        myconfig = new Config(this);
        scllview.setBackgroundDrawable(myconfig.return_bg());
        LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        Llay.setOrientation(LinearLayout.VERTICAL);
        
    	String cookie_data =new_cookie;
    	//http://www.dk101.com/Discuz/viewthread.php?tid=186169&extra=page%3D1&page=1
    	String tmp_dest = Utility.return_webSite2Root()+forum_url+"&page="+String.valueOf(curr_page);
    	TCPGather mytcpgather = new TCPGather(tmp_dest,cookie_data);
    	
		if(mytcpgather.AskForHtml())
    	{
    		ArrayList<String> html_result = mytcpgather.GetTheResult();
    		boolean find_the_forumpage = false;
    		for(int i=0;i<html_result.size();i++)
    		{
    			if(html_result.get(i).matches(".*<[Dd][Ii][Vv] *[Cc][Ll][Aa][Ss][Ss]=\"[Pp][Aa][Gg][Ee][Ss]\">.*")&&!find_the_forumpage)
    			{
    				find_the_forumpage=true;
    				System.out.println(html_result.get(i));
    				if(html_result.get(i).matches(".*\\.\\.\\..*"))
    				{
    					try
    					{
    					total_page = Integer.parseInt(html_result.get(i).replaceFirst(".*\\.\\.\\.", "").split("</[Aa]>")[0].replaceAll(" ", ""));
    					}
    					catch(Exception e)
    					{
    						Utility.quick_dialog("Error in fetching total number of page", this);
    						total_page=1;
    					}
    				}
    				else
    				{
    					try
    					{
    					String[] split_tmp_str = html_result.get(i).replaceFirst("&rsaquo;&rsaquo;.*", "").split("</a>");
    					total_page=Integer.parseInt(split_tmp_str[split_tmp_str.length-2].replaceFirst("<[Aa] *[Hh][Rr][Ee][Ff].*\">", "").replaceFirst(" ", ""));
    					}
    					catch(Exception e)
    					{
    						Utility.quick_dialog("Error in fetching total number of page", this);
    						total_page=1;
    					}
    				}
    			}
    			//System.out.println(html_result.get(i));
    		}
    		if(this.total_page==0)
    		{
				Utility.quick_dialog("Error in fetching total number of page", this);
				total_page=1;
    		}
    		for(int i=0;i<total_page;i++)
    		{
		     	   LinearLayout tmpLayout = new LinearLayout(this);
		            LinearLayout.LayoutParams tmpLayout_param = new LinearLayout.LayoutParams(
		            		ViewGroup.LayoutParams.FILL_PARENT,
		            		ViewGroup.LayoutParams.WRAP_CONTENT);
		            TextView tmp_txtview = new TextView(this);
		            tmp_txtview.setText("Page: "+String.valueOf(i+1));
		            tmp_txtview.setTextColor(Color.BLACK);
		            tmp_txtview.setTextSize(TypedValue.COMPLEX_UNIT_SP,(float)21);
		            PanelOnTouchListener potl =  new PanelOnTouchListener(tmp_txtview);
		            tmpLayout.setClickable(true);
		            tmpLayout.setOnTouchListener(potl);
		            InternetForumChListener click_listener = new InternetForumChListener(this,this.forum_name,forum_url,i+1,new_cookie);
		        	//InternetBookClickListener click_listener = new InternetBookClickListener(this,bookname.get(i),this.bookurl.get(i),this.bookid.get(i));
		        	tmpLayout.setOnClickListener(click_listener);
		        	tmpLayout.addView(tmp_txtview);
		     	   Llay.addView(tmpLayout,tmpLayout_param);
    		}
    	}
		Utility.quick_dialog(String.valueOf(this.total_page), this);
        scllview.addView(Llay, Llay_param);

        this.setTitle(forum_name);

        this.setContentView(scllview, scllview_param);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 0, 0, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert);
    	return super.onCreateOptionsMenu(menu); 
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId()==0)
    	{
    		Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
    		this.startActivity(int_ent);
    		finish();
    	}
    	return super.onOptionsItemSelected(item); 
    }
}
